structure Parse : sig val parse : string -> Absyn.exp
		      val main : string * string list -> OS.Process.status end =
struct 
  structure TigerLrVals = TigerLrValsFun(structure Token = LrParser.Token)

  structure TigerLex = TigerLexFun(structure Tokens = TigerLrVals.Tokens)

  structure TigerParser = Join(structure ParserData = TigerLrVals.ParserData
                               structure Lex = TigerLex
			       structure LrParser = LrParser)


  fun parse filename =
      let val _ = ( ErrorMsg.reset()
		  ; ErrorMsg.fileName := filename)
	  val file = TextIO.openIn filename
	  fun get _ = TextIO.input file
	  fun parseError(s,p1,p2) = ErrorMsg.error p1 s
	  val lexer = LrParser.Stream.streamify (TigerLex.makeLexer get)
	  val (absyn, _) = TigerParser.parse(30,lexer,parseError,())
      in TextIO.closeIn file;
	 absyn
      end handle LrParser.ParseError => raise ErrorMsg.Error

  fun process(p, fs) = let fun pr f = ( TextIO.output (TextIO.stdErr, f)
				      ; TextIO.output (TextIO.stdErr, "\n")
				      ; (p f
					 handle ErrorMsg.Error => TextIO.output (TextIO.stdErr, "parse failed, continuing...\n"))
				      ; OS.Process.success )
		       in let fun loop [] = OS.Process.success
				| loop [f] = pr f
				| loop (f::fs) = ( pr f
						 ; loop fs )
			  in loop fs
			  end
		       end;

  fun main (_, ("-a" ::  fs)) = process (fn f => PrintAbsyn.print (TextIO.stdOut, (parse f)), fs)
    | main (_, ("-u" ::  fs)) = process (fn f => UnparseAbsyn.print (TextIO.stdOut, (parse f)), fs)
    | main (_, (c ::  fs)) = ( print ("unrecognized command: " ^ c ^ "\n")
				  ; print "usage: -atu <filenames>\n"
				  ; OS.Process.failure )
    | main (_, _) = ( print "usage: -au <filenames>\n"
		    ; OS.Process.failure )
end
